//
// Created by xyb on 16/2/26.
//
#include <jni.h>
/* Header for class com_uhylab_uhuvinetest_jniopen_JniOpen */


#include <stdio.h>
#include <stdlib.h>
#include <jni.h>
#include <opencv2/opencv.hpp>
#include <algorithm>
#include <string>
#include <vector>
#include "SquareDetect_API.h"
#include "UHY_TimeCnt.h"

using namespace std;
using namespace cv;

#include "ScanColor.h"

#include <android/log.h>

#define LOG_TAG "FaceDetection/DetectionBasedTracker"
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))

#ifndef SCAN_COLOR
#define SCAN_COLOR
ScanColor scanColor;
#endif

CUHYRecogCls uhyrecog;
int numberTime;
Mat mBGR;
RecogResult uhyres;

extern "C" {

JNIEXPORT jboolean JNICALL Java_com_uhylab_uhuvinetest_camera_CaptureActivity_FindFeatures(
        JNIEnv *env, jobject obj, jlong addrGray, jlong addrRgba, jlong jint1, jint width,
        jint height);

JNIEXPORT jboolean JNICALL Java_com_uhylab_uhuvinetest_activity_UHPhotoActivity_FindFeatures(
        JNIEnv *env, jobject obj, jlong addrRgba);

JNIEXPORT jboolean JNICALL Java_com_uhylab_uhuvinetest_activity_UHPhotoActivity_DetectInit(
        JNIEnv *env, jobject obj, jstring path);

JNIEXPORT jboolean JNICALL Java_com_uhylab_uhuvinetest_activity_UHPhotoActivity_DetectRelease(
        JNIEnv *env, jobject obj);

JNIEXPORT jboolean JNICALL Java_com_uhylab_uhuvinetest_camera_CaptureActivity_DetectInit(
        JNIEnv *env, jobject obj, jstring path);

JNIEXPORT jboolean JNICALL Java_com_uhylab_uhuvinetest_camera_CaptureActivity_DetectRelease(
        JNIEnv *env, jobject obj);

JNIEXPORT jboolean JNICALL Java_com_uhylab_uhuvinetest_activity_UHPhotoActivity_DetectRelease(
        JNIEnv *env, jobject obj) {
    uhyrecog.DetectRelease();
} ;
JNIEXPORT jboolean JNICALL Java_com_uhylab_uhuvinetest_activity_UHPhotoActivity_DetectInit(
        JNIEnv *env, jobject obj, jstring path) {
    char *s_path = (char *) env->GetStringUTFChars(path, 0);
    uhyrecog.DetectInit(s_path);
    numberTime = 1;
} ;

JNIEXPORT jboolean JNICALL Java_com_uhylab_uhuvinetest_activity_UHPhotoActivity_FindFeatures(
        JNIEnv *env, jobject obj, jlong addrRgba) {
    Mat &img = *(Mat *) addrRgba;
    Mat BGR;
    cvtColor(img, BGR, CV_RGBA2BGR);
    unsigned char b = 0;
    Mat _imgt;
    Mat _imgtm;
    transpose(BGR, _imgt);
    flip(_imgt, _imgt, 1);
    IplImage imgTmp = _imgt;
    IplImage *ipl_img = cvCloneImage(&imgTmp);
    RecogResult uhyres;
    if (uhyrecog.SquareDetectRecog((unsigned char *) ipl_img->imageData, ipl_img->width,
                                   ipl_img->height, uhyres)) {

        jfieldID arrColorId;

        jclass cls = env->GetObjectClass(obj);
        arrColorId = env->GetFieldID(cls, "arrcolor", "[F");

        jfloat colorsFloat[48];
        jfloatArray jflArray = env->NewFloatArray(48);

        for (int ii = 0; ii < 11; ii++) {
            colorsFloat[ii * 4 + 0] = uhyres.realRect[ii].left;
            colorsFloat[ii * 4 + 1] = uhyres.realRect[ii].top;
            colorsFloat[ii * 4 + 2] = uhyres.realRect[ii].right;
            colorsFloat[ii * 4 + 3] = uhyres.realRect[ii].bottom;
        }
        if (numberTime == 1) {
            numberTime == 2;
            scanColor.UpdatBuff((unsigned char *) ipl_img->imageData, ipl_img->width,
                                ipl_img->height, 3, uhyres);
            for (int iii = 0; iii < 11; iii++) {
                int ID_ = scanColor.colorEdit.colorBlock[iii].ID;
            }
        }
        colorsFloat[44] = uhyres.stdRect[1].left;
        colorsFloat[45] =
                uhyres.stdRect[1].top - (uhyres.stdRect[1].bottom - uhyres.stdRect[1].top);
        colorsFloat[46] = uhyres.stdRect[1].right;
        colorsFloat[47] =
                uhyres.stdRect[1].bottom - (uhyres.stdRect[1].bottom - uhyres.stdRect[1].top);

        // 设置float数组
        env->SetFloatArrayRegion(jflArray, 0, 48, colorsFloat);
        env->SetObjectField(obj, arrColorId, jflArray);
        b = 1;

    }

    return (jboolean) b;
} ;

JNIEXPORT jboolean JNICALL Java_com_uhylab_uhuvinetest_camera_CaptureActivity_DetectInit(
        JNIEnv *env, jobject obj, jstring path) {
    char *s_path = (char *) env->GetStringUTFChars(path, 0);
    uhyrecog.DetectInit(s_path);
    numberTime = 1;
} ;

JNIEXPORT jboolean JNICALL Java_com_uhylab_uhuvinetest_camera_CaptureActivity_DetectRelease(
        JNIEnv *env, jobject obj) {
    uhyrecog.DetectRelease();
} ;


JNIEXPORT jboolean JNICALL Java_com_uhylab_uhuvinetest_camera_CaptureActivity_FindFeatures(
        JNIEnv *env, jobject obj, jlong addrGray, jlong addrRgba, jlong jint1, jint caWidth,
        jint caHeight) {
    Mat &img = *(Mat *) addrRgba;
    cvtColor(img, mBGR, CV_RGBA2BGR);
    unsigned char b = 0;
    // apply perspective transformation
    Mat _imgt;
    Mat _imgtm;
    transpose(mBGR, _imgt);
    flip(_imgt, _imgt, 1);

    IplImage imgTmp = _imgt;
    IplImage *ipl_img = cvCloneImage(&imgTmp);


    // 进行图片预处理

    // 判断图片的亮度
    if (uhyrecog.BrightDetect((unsigned char *) ipl_img->imageData, ipl_img->width, ipl_img->height,
                              60, 230)) {

        if (uhyrecog.SquareDetectRecog((unsigned char *) ipl_img->imageData, ipl_img->width,
                                       ipl_img->height, uhyres)) {
            jfieldID leftid;
            jfieldID rightid;
            jfieldID topid;
            jfieldID bottomid;
            jfieldID arrColorId;
            jfieldID weidongId;
            jfieldID liumingId;

            jclass cls = env->GetObjectClass(obj);
            leftid = env->GetFieldID(cls, "left", "Z");
            rightid = env->GetFieldID(cls, "right", "Z");
            topid = env->GetFieldID(cls, "top", "Z");
            bottomid = env->GetFieldID(cls, "bottom", "Z");
            arrColorId = env->GetFieldID(cls, "arrcolor", "[F");
            weidongId = env->GetFieldID(cls, "weidong", "[I");
            liumingId = env->GetFieldID(cls, "liuming", "[I");

            jfloat colorsFloat[48];
            jint weidongFloat[11];
            jint liumingFloat[11];
            jfloatArray jflArray = env->NewFloatArray(48);
            jintArray weidongArray = env->NewIntArray(11);
            jintArray liumingArray = env->NewIntArray(11);
            transpose(img, img);
            flip(img, img, 1);
            if (numberTime == 1) {
                numberTime == 2;
                scanColor.UpdatBuff((unsigned char *) ipl_img->imageData, ipl_img->width,
                                    ipl_img->height, 3, uhyres);
                for (int iii = 0; iii < 11; iii++) {
                    int ID_ = scanColor.colorEdit.colorBlock[iii].ID;
                    weidongFloat[iii] = ID_;
                }
            }


            for (int ii = 0; ii < 11; ii++) {
                rectangle(img, Point(uhyres.realRect[ii].left, uhyres.realRect[ii].top),
                          Point(uhyres.realRect[ii].right, uhyres.realRect[ii].bottom),
                          Scalar(0, 255, 0), 2);

                circle(img, Point((uhyres.realRect[ii].right - uhyres.realRect[ii].left) / 2 +
                                  uhyres.realRect[ii].left,
                                  (uhyres.realRect[ii].bottom - uhyres.realRect[ii].top) / 2 +
                                  uhyres.realRect[ii].top), 5, Scalar(255, 0, 0));
                colorsFloat[ii * 4 + 0] = uhyres.realRect[ii].left;
                colorsFloat[ii * 4 + 1] = uhyres.realRect[ii].top;
                colorsFloat[ii * 4 + 2] = uhyres.realRect[ii].right;
                colorsFloat[ii * 4 + 3] = uhyres.realRect[ii].bottom;
            }

            for (int nine = 0; nine < 2; ++nine) {
                rectangle(img, Point(uhyres.stdRect[nine].left, uhyres.stdRect[nine].top),
                          Point(uhyres.stdRect[nine].right, uhyres.stdRect[nine].bottom),
                          Scalar(0, 255, 0), 1);
            }
            colorsFloat[44] = uhyres.stdRect[1].left;
            colorsFloat[45] =
                    uhyres.stdRect[1].top - (uhyres.stdRect[1].bottom - uhyres.stdRect[1].top);
            colorsFloat[46] = uhyres.stdRect[1].right;
            colorsFloat[47] =
                    uhyres.stdRect[1].bottom - (uhyres.stdRect[1].bottom - uhyres.stdRect[1].top);
            liumingFloat[0] = uhyres.baixibao_idx;
            liumingFloat[1] = uhyres.yaxiaosuanyan_idx;
            liumingFloat[2] = uhyres.niaodanyuan_idx;
            liumingFloat[3] = uhyres.danbaizhi_idx;
            liumingFloat[4] = uhyres.suanjiandu_idx;
            liumingFloat[5] = uhyres.qianxue_idx;
            liumingFloat[6] = uhyres.bizhong_idx;
            liumingFloat[7] = uhyres.tongti_idx;
            liumingFloat[8] = uhyres.danhongsu_idx;
            liumingFloat[9] = uhyres.putaotang_idx;
            liumingFloat[10] = uhyres.kanghuaixuesuan;
            transpose(img, img);
            flip(img, img, 0);

            env->SetBooleanField(obj, topid, JNI_TRUE);
            env->SetBooleanField(obj, leftid, JNI_TRUE);
            env->SetBooleanField(obj, rightid, JNI_TRUE);
            env->SetBooleanField(obj, bottomid, JNI_TRUE);
            // 设置float数组
            env->SetFloatArrayRegion(jflArray, 0, 48, colorsFloat);
            env->SetObjectField(obj, arrColorId, jflArray);
            env->SetIntArrayRegion(weidongArray, 0, 11, weidongFloat);
            env->SetObjectField(obj, weidongId, weidongArray);
            env->SetIntArrayRegion(liumingArray, 0, 11, liumingFloat);
            env->SetObjectField(obj, liumingId, liumingArray);
            b = 1;

        }
    }
    // if(b == 0){
    // mBGR.release();
    // }
    return (jboolean) b;

}
}